global class MobileAppTransactionTypesExt
{
    public List<AggregateResult> applicationPackageList {get; set;}
    
    public MobileAppTransactionTypesExt()
    {
        
    }
    
    public MobileAppTransactionTypesExt(ApexPages.StandardController controller) 
    {   
        applicationPackageList = [SELECT Application_Package__c 
                                  FROM Transaction_Types__c 
                                  WHERE isMobile__c = True AND Active__c = True 
                                  GROUP BY Application_Package__c
                                  ORDER BY Application_Package__c];
    }
    
    // Code we will invoke on page load.
    public PageReference forwardToCustomAuthPage()
    {
        if(UserInfo.getUserType() == 'Guest')
        {
            return new PageReference('/MobileAppCustomLogin');
        }
        else
        {
            return null;
        }
    }
    
    @RemoteAction
    global static List<AggregateResult> getApplicationTypesList (String selectedApplicationPackage) {
        List<AggregateResult> applicationTypesList = [SELECT Application_Type__c 
                                                        FROM Transaction_Types__c 
                                                        WHERE isMobile__c = True AND Active__c = True 
                                                        AND Application_Package__c = :selectedApplicationPackage 
                                                        GROUP BY Application_Type__c
                                                        ORDER BY Application_Type__c];
        return applicationTypesList;
    }
    
    @RemoteAction
    global static List<Transaction_Types__c> getApplicationDescriptionsList (String selectedApplicationPackage, 
                                                                    String selectedApplicationType) {
        List<Transaction_Types__c> applicationDescriptionList;
        applicationDescriptionList = [SELECT Id, Transaction_Description__c 
                                        FROM Transaction_Types__c 
                                        WHERE isMobile__c = True AND Active__c = True  
                                        AND Application_Package__c = :selectedApplicationPackage 
                                        AND Application_Type__c = :selectedApplicationType
                                        ORDER BY Transaction_Description__c];
        return applicationDescriptionList;
    }
    
    @RemoteAction
    global static String navigateToEditPage(String selectedTransactionDescription)
    {
        List<Transaction_Types__c> transactionTypesList;
        
        transactionTypesList = [SELECT Id, Page_Name__c
                                FROM Transaction_Types__c
                                WHERE isMobile__c = True AND Active__c = True
                                AND Id = :selectedTransactionDescription
                                LIMIT 1];
        
        string queryParameters = 'transactionTypeId=' + transactionTypesList[0].Id;
        
        return ('/apex/' + transactionTypesList[0].Page_Name__c + '?' + queryParameters);
    }
    
}